package com.ptszyxx.popose.module.main.trip;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import com.github.gzuliyujiang.wheelpicker.OptionPicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentTripReleaseBinding;
import com.ptszyxx.popose.module.main.trip.vm.TripReleaseVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YPermissionUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.widget.pic.SelectPicAdapter;
import com.ysg.widget.pic.SelectPicBean;
import com.ysg.widget.pic.SelectPicEnum;
import com.ysg.widget.pic.YPicUtils;

import java.util.Arrays;
import java.util.List;

public class TripReleaseFragment extends BaseFragment<FragmentTripReleaseBinding, TripReleaseVM> {
    private SelectPicAdapter adapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_trip_release;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public TripReleaseVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(TripReleaseVM.class);
    }

    @Override
    public void initAdapter() {
        adapter = new SelectPicAdapter(getContext(), SelectPicEnum.select);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        binding.recyclerView.setAdapter(adapter);

        //记得添加权限：
        adapter.setOnSelectListener(new SelectPicAdapter.OnSelectPicListener() {
            @Override
            public void onAddPicListener() {
                YPicUtils.getInstance().addAlbumAndVideo(getContext(), "申请拍照权限：用于发布旅途动态图片",adapter, (result)->{
                    //这边要处理下：正常先上传图片后显示
                    for (LocalMedia item: result) {
                        SelectPicBean picBean = new SelectPicBean();
                        if (PictureMimeType.isHasVideo(item.getMimeType())){
                            picBean.setPic(item.getRealPath());
                            picBean.setMinPic(item.getRealPath());
                        }else{
                            if (YStringUtil.isNotEmpty(item.getCompressPath())){
                                picBean.setPic(item.getCompressPath());
                                picBean.setMinPic(item.getCompressPath());
                            }else{
                                picBean.setPic(item.getRealPath());
                                picBean.setMinPic(item.getRealPath());
                            }
                        }
                        adapter.getData().add(picBean);
                    }
                    adapter.notifyDataSetChanged();
                });
            }

            @Override
            public void onDeletePicListener(int position) {
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestOosToken();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onNameEvent.observe(this, o -> showDialogName());
        viewModel.uc.onSetOutTimeEvent.observe(this, o -> showDialogSetOutTime());
        viewModel.uc.onReachTimeEvent.observe(this, o -> showDialogReachTime());
        viewModel.uc.onTravelEvent.observe(this, o -> showDialogTravel());
        viewModel.uc.onSubmitEvent.observe(this, o -> onSubmit());
    }

    /**
     * 活动对象
     */
    private void showDialogTravel() {
        List<String> list = Arrays.asList(getResources().getStringArray(R.array.trip_list_travel));
        OptionPicker picker = new OptionPicker(getActivity());
        picker.setData(list);
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.travel.set(list.get(position));
            }
        });
        picker.show();
    }
    /**
     * 活动对象
     */
    private void showDialogReachTime() {
        List<String> list = Arrays.asList(getResources().getStringArray(R.array.trip_list_reach_time));
        OptionPicker picker = new OptionPicker(getActivity());
        picker.setData(list);
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.reachTime.set(list.get(position));
            }
        });
        picker.show();
    }
    /**
     * 活动对象
     */
    private void showDialogSetOutTime() {
        List<String> list = Arrays.asList(getResources().getStringArray(R.array.trip_list_set_out_time));
        OptionPicker picker = new OptionPicker(getActivity());
        picker.setData(list);
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.setOutTime.set(list.get(position));
            }
        });
        picker.show();
    }

    /**
     * 活动对象
     */
    private void showDialogName() {
        List<String> list = Arrays.asList(getResources().getStringArray(R.array.trip_list_name));
        OptionPicker picker = new OptionPicker(getActivity());
        picker.setData(list);
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.name.set(list.get(position));
            }
        });
        picker.show();
    }

    private void onSubmit() {
        if (YCollectionUtil.isEmpty(adapter.getData())){
            YToastUtil.showShort(R.string.tip_select_pic);
            return;
        }
        viewModel.requestUploadPic(adapter.getData());
    }

}
